package com.yycx.module.bus.provider.handler.process.listener;

import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusOrderDetails;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.BusOrderDetailsService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 朗迪回款审批结束监听器
 *
 * @author zyf
 */
@Component
public class LdHuikuanProcessEndListener implements ExecutionListener {

    @Resource
    private BusPaymentService paymentService;

    @Resource
    private BusOrderDetailsService orderDetailsService;
    @Override
    public void notify(DelegateExecution delegateExecution) {
        String businessKey = delegateExecution.getProcessInstanceBusinessKey();
        BusPayment payment = paymentService.getById(businessKey);
        BusOrderDetails orderDetails = orderDetailsService.getById(payment.getOrderDetailsId());
        ApiAssert.isNotEmpty("回款信息不存在", orderDetails);

        payment.setPaymentState(CommonConstants.INT_2);
        //更新为已审批状态
        paymentService.updateById(payment);

        //更新明细表中回款信息
        orderDetailsService.updateHuikuanInfo(payment);

        BusOrderDetails piaozhe=orderDetailsService.selectPiaozhe(orderDetails.getOrderId());
        //更新票折回款
        if(FlymeUtils.isNotEmpty(piaozhe)){
            piaozhe.setReturnAmount(piaozhe.getSaleAmount());
            piaozhe.setReturnDate(DateUtils.formatDate(payment.getAmountDate(), "yyyy-MM-dd"));
            piaozhe.setRenkuanDate(DateUtils.getYear() + "-" + DateUtils.getMonth());
            piaozhe.setCustomReturnState(CommonConstants.INT_2.toString());
            piaozhe.setReturnId(payment.getPaymentId());
            orderDetailsService.saveOrUpdate(piaozhe);
        }

    }
}
